﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Vergnaty.Repository.Model;
using Vergnaty.Repository;
using System.Configuration;

namespace Vergnaty.UnitTest
{
    [TestClass]
    public class UserRepositoryTest
    {
        IRepository<User> repository;

        [TestInitialize]
        public void Init()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
            this.repository = new UserRepository(connectionString);
        }

        [TestMethod]
        public void InsertUserTestSuccess()
        {
            //SETUP TEST
            var user = FakeData.GetUser();
            this.repository.Add(user);

            //GET USER INSERTED
            var userInserted = this.repository.GetById(user.Id);

            //Assert 
            Assert.IsNotNull(userInserted);
            Assert.AreEqual(user.Id, userInserted.Id);
            Assert.AreEqual(user.FistName, userInserted.FistName);
            Assert.AreEqual(user.LastName, userInserted.LastName);
            Assert.AreEqual(user.Email, userInserted.Email);
            Assert.AreEqual(user.Age, userInserted.Age);

            //Clear fake data
            this.repository.Delete(user.Id);
        }

        [TestMethod]
        public void UpdateUserTestSuccess()
        {
            //SETUP TEST
            var user = FakeData.GetUser();
            this.repository.Add(user);

            user.Email = "vergnaty@codeplax.com";
            user.Age = 20;

            //update user
            this.repository.Update(user);

            //GET USER UPDATED
            var userUpdated = this.repository.GetById(user.Id);

            //Assert 
            Assert.IsNotNull(userUpdated);
            Assert.AreEqual(user.Email, userUpdated.Email);
            Assert.AreEqual(user.Age, userUpdated.Age);

            //Clear fake data
            this.repository.Delete(user.Id);
        }

        [TestMethod]
        public void GetListUserTestSuccess()
        {
            var user1 = FakeData.GetUser();
            var user2 = FakeData.GetUser();

            this.repository.Add(user1);
            this.repository.Add(user2);

            //the age is equale for each user add
            User userFilter = new User()
            {
                Age = user1.Age
            };

            //get list
            var results = repository.GetList(userFilter);

            //ASSERT
            Assert.IsNotNull(results);
            Assert.AreEqual(2, results.Count);

            //delete fake datas
            this.repository.Delete(user1.Id);
            this.repository.Delete(user2.Id);
        }
    }

    public static class FakeData
    {
        public static User GetUser()
        {
            return new User()
            {
                Age = 25,
                Email = "natnael.mekuannt@gmail.com",
                FistName = "Natnael",
                LastName = "Getu",
                Id = Guid.NewGuid()
            };
        }
    }
}
